package org.example.datastructure.ch3;

import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class StackApplication {
    public boolean isReverse(String str){
        LinkedList<Character> stack = Arrays.asList(str)
                .stream()
                .map(word->word.split(""))
                .flatMap(Arrays::stream)
                .map(ss->ss.charAt(0))
                .collect(Collectors.toCollection(LinkedList::new));
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)!=(char)stack.pollLast()){
                return  false;
            }
        }
        return true;
    }

    private boolean isMatch(char c1, char c2){
        if(c1=='('&&c2==')'||c1=='{'&&c2=='}'||c1=='['&&c2==']'){
            return true;
        }
        return false;
    }
    public boolean checkBrackets(String str){
        LinkedList<Character> stack = new LinkedList<>();
        for(int i=0;i<str.length();i++){
            char ch = str.charAt(i);
            if(ch=='('||ch=='{'||ch=='['){
                stack.add(ch);
            }
            else{
                if(stack.isEmpty()){
                    return false;
                }
                char top = stack.pollLast();
                if(!isMatch(top,ch)){
                    return false;
                }
            }
        }
        if(!stack.isEmpty()) return false;
        return true;
    }
    private int count(int num1,int num2 ,char op){
        if(op=='+') return num1+num2;
        if(op=='-') return num1-num2;
        if(op=='*') return num1*num2;
        if(op=='/') return num1/num2;
        return 0;
    }
    public int calculate(String str){
        Stack<Character> stack = new Stack<>();
        Stack<Integer> stack1 = new Stack<>();
        List<Character> list = new ArrayList<>();
        int n = str.length();
        for(int i=0;i<n;i++){
            char ch_pop = stack.empty()?'#':stack.peek();
            char ch = str.charAt(i);
            if(ch=='+'||ch=='-'){
                while(!stack.empty()&&ch_pop!='('){
                    ch_pop = stack.pop();
                    list.add(ch_pop);
                }
                stack.add(ch);
            }
            if(ch=='*'||ch=='/'){
                while(ch_pop!='+'&&ch_pop!='-'&&ch_pop!='('&&!stack.empty()){
                    ch_pop = stack.pop();
                    list.add(ch_pop);
                    ch_pop = stack.peek();
                }
                stack.add(ch);
            }
            if(ch=='('){
                stack.push(ch);
            }
            if(ch==')'){
                ch_pop = stack.pop();
                while(ch_pop!='('){
                    list.add(ch_pop);
                    ch_pop = stack.pop();

                }
            }
            if(ch>='0'&&ch<='9'){
                list.add(ch);
            }
        }
        while(!stack.empty()){
            list.add(stack.pop());
        }
        System.out.println(list);
        for(Character cch:list){
            if(cch>='0'&&cch<='9'){
                stack1.push((int)(cch-'0'));
            }else{
                int ch2 = stack1.pop();
                int ch1 = stack1.pop();
                int res = count(ch1,ch2,cch);
                stack1.push(res);
            }
        }
        return stack1.pop();
    }
}
